package com.badlogic.androidgames;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.TextView;

public class MediaPlayerTest extends Activity{
	
	MediaPlayer mediaPlayer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView tv = new TextView(this);
		setContentView(tv);
		
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		mediaPlayer = new MediaPlayer();
		try {
			AssetManager assetManager = getAssets();
			AssetFileDescriptor descriptor = assetManager.openFd("bitten.ogg");
			mediaPlayer.setDataSource(descriptor.getFileDescriptor(),
					descriptor.getStartOffset(),descriptor.getLength());
			mediaPlayer.prepare();
			mediaPlayer.setLooping(true);
		} catch (IOException e) {
			tv.setText("Couldn't load music file, " + e.getMessage());
			mediaPlayer = null;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(mediaPlayer != null)
			mediaPlayer.start();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if(mediaPlayer != null){
			mediaPlayer.pause();
			if(isFinishing()){
				mediaPlayer.stop();
				mediaPlayer.release();
			}
		}
	}

}
